package com.lottery.material.controller.admin.system;

import com.lottery.material.common.JsonResult;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.entity.admin.Role;
import com.lottery.material.service.admin.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Description：角色Controller
 * Created by 朱正磊 on 2019-03-26 16:55
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

    private final String PATH = "admin/role/";

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * description：请求访问角色主页
     * user 朱正磊
     * time 2019-03-27 09:21
     * @return 返回模型视图
     */
    @GetMapping(value = {"/", "/index"})
    public ModelAndView index() {
        return new ModelAndView(PATH + "index");
    }

    /**
     * description：查询角色列表（分页）
     * user 朱正磊
     * time 2019-03-27 09:24
     * @param searchText 查询条件
     * @return 返回响应结果
     */
    @PostMapping("/list")
    @ResponseBody
    public Page<Role> list(@RequestParam(value = "searchText", required = false) String searchText) {
        return roleService.findAllByLike(searchText, getPageRequest());
    }

    /**
     * description：请求访问角色新增表单页
     * user 朱正磊
     * time 2019-03-27 10:06
     * @return 返回模型视图
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return new ModelAndView(PATH + "form");
    }

    /**
     * description：请求访问角色修改表单页
     * user 朱正磊
     * time 2019-03-27 10:10
     * @param id 角色ID
     * @param map 响应数据
     * @return 返回模型视图
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id, Map<String, Object> map) {
        Role role = roleService.findById(id);
        map.put("role", role);
        return new ModelAndView(PATH + "form", map);
    }

    /**
     * description：增加或者修改角色
     * user 朱正磊
     * time 2019-03-27 10:14
     * @param role 角色对象
     * @return 返回响应结果
     */
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult edit(Role role) {
        try {
            roleService.saveOrUpdate(role);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * description：根据主键删除一条记录
     * user 朱正磊
     * time 2019-03-27 10:23
     * @param id 角色ID
     * @return 返回响应结果
     */
    @GetMapping("/delete/{id}")
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id) {
        try {
            roleService.delete(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * description：访问给角色分配资源页面
     * user 朱正磊
     * time 2019-03-27 10:35
     * @param id 角色ID
     * @param map 响应数据
     * @return 返回模型视图
     */
    @GetMapping("/grant/{id}")
    public ModelAndView grant(@PathVariable Integer id, Map<String, Object> map) {
        Role role = roleService.findById(id);
        map.put("role", role);
        return new ModelAndView(PATH + "grant", map);
    }

    /**
     * description：给角色分配资源
     * user 朱正磊
     * time 2019-03-27 10:35
     * @param id 角色ID
     * @param resourceIds 资源ID数组
     * @return 返回响应结果
     */
    @PostMapping("/grant/{id}")
    @ResponseBody
    public JsonResult grant(@PathVariable Integer id, String[] resourceIds) {
        try {
            roleService.grant(id, resourceIds);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

}
